
*Drop all existing interactions because they need to be regenerated with correct leads and lags AFTER stacked data is generated 
cap drop years_since_* 
cap drop pre_* 
cap drop post_* 
cap drop p1_* 
cap drop p2_* 
cap drop p3_* 
cap drop noson_*  
cap drop year_1* 

*only requires year id LLFyear - but should keep any data that we would want to persist

*Identify never-treated: here we only have last treated 
	gen never = (LLFyear==0)
				
*Identify not-yet treated for each treatment cohort
	levelsof LLFyear, local(cohorts)
	foreach x of local cohorts { 
		preserve
		gen treat = 1 if LLFyear ==`x'
		gen notyet`x' = 1 if LLFyear==0
		replace notyet`x' = 1 if (LLFyear > `x')
		replace notyet`x' = 0 if missing(notyet`x')==1		
		label var notyet`x' "Not Yet Treated Control for Treated Cohort `x'"	
		keep if notyet`x' == 1 | treat == 1 | never == 1
		*event_T = 1 equals first year of LLF
		gen event_T = year- `x' + 1
		replace treat = 0 if missing(treat)
		gen StackDataID = `x'
		tempfile Stack`x'
		save `"`Stack`x'''"', replace
		restore
	}	

*Stack clean 2x2 datasets 
	clear 
	tempfile stackdata 
	save `stackdata', emptyok
	foreach x of local cohorts { 
				append using `"`Stack`x'''"'
				save `"`stackdata'"' , replace
			}
	label var treat "Treatment Group"
	label var never "Never Treated Group"
	label var StackDataID "Treatment Cohort"

	order event_T StackDataID, last		


*Drop control groups where years are treated by LLF
	drop if treat==0 & (year>=LLFyear)

*Examine available event times for treat and control groups

	tab event_T StackDataID if treat==1
	tab event_T StackDataID if treat==0

	egen FE_t  = group(year StackDataID)
	egen FE_prov = group(prov StackDataID)
*	egen FE_id = group(id StackDataID)

*Examine event times with available treatment and control 
	tab event_T treat 

*Keep event times with both available treatment and control groups 
	*Old code
	*keep if inrange(event_T, -7,9)
	
	*New code (11/25/2022)
	gen BalancedData = 0 
	replace BalancedData = 1 if inrange(event_T,-4,9) & StackDataID==1969
	replace BalancedData = 1 if inrange(event_T,-5,9) & StackDataID==1970
	replace BalancedData = 1 if inrange(event_T,-6,8) & StackDataID==1971
	replace BalancedData = 1 if inrange(event_T,-7,7) & StackDataID==1972
	replace BalancedData = 1 if inrange(event_T,-7,6) & StackDataID==1973
	replace BalancedData = 1 if inrange(event_T,-7,5) & StackDataID==1974
	replace BalancedData = 1 if inrange(event_T,-4,4) & StackDataID==1975
	keep if BalancedData ==1
	drop BalancedData 

*Visual check again - should not have any zeros
	bysort StackDataID: tab event_T treat 
************************************************************************

*Generate Leads and Lags

	gen Leads = event_T if event_T>0 
	gen Lags = abs(event_T) if event_T<=0 

	tab Leads, gen(Leads_)

	tab Lags, gen(Lags_)

	foreach x of varlist Leads_* Lags_*  { 
		replace `x' = 0 if missing(`x')
		replace `x' = `x'*(StackDataID==LLFyear) //time indicator for the "treated" firms in each stack
	}

	compress 


